<template>
	<view class="page-wrapper">
		<div class="go-back" @click="goBack">
			<image  src="../../static/img/public/left.png" :style="'width: 19rpx; position: absolute; left: 30rpx; top:' + (navH - 25) + 'px;' " mode="widthFix"></image>
		</div>
		<div class="content">
			<div>
				<image src="../../static/img/public/logo2.png" mode="widthFix"></image>
				
				<button v-if="userInfo === null" open-type="getUserInfo" @getuserinfo="bindGetUserInfo" >授权登录</button>
				<!-- <div v-if="userInfo === null" class="am-ft-gray am-ft-14" style="box-sizing: border-box; padding:20rpx 96rpx;font-weight: 300;" :style="userInfo!==null?'opacity:0;':''">
					您暂未授权店拼购小程序获取你的信息，将无法正常使用小程序会员功能。如需正常使用，请点击“授权”按钮，打开头像，昵称等信息的授权。
				</div> -->
				<button v-if="userInfo !== null" open-type="getPhoneNumber" @getphonenumber="bindGetPhoneNumber" >微信用户快捷登录</button>
			</div>
		</div>
		<p>极致 · 专业 · 诚信 · 舒适</p>
	</view>
</template>

<script>
	import {api} from '@/util/api.js';
	export default {
		data() {
			return {
				openid: null,
				userInfo: null,
				encryptedData: null,
				iv: null,
				navH: 0,
				from: ''
			}
		},
		methods: {
			goBack() {
				// if(this.from !== '') {
				// 	this.from === '';
				// 	wx.reLaunch({
				// 	  url: '../index/index'
				// 	})
				// }
				uni.navigateBack();
			},
			
			bindGetUserInfo: function(e) {
				this.userInfo = e.detail.userInfo;
			},
			bindGetPhoneNumber: function(e) {
				if(e.detail.errMsg === 'getPhoneNumber:ok') {
					this.encryptedData = e.detail.encryptedData;
					this.iv = e.detail.iv;
					this.register();
				}
			},
			
			
			register: function() {
				// if (this.openid == null || this.userInfo == null || this.encryptedData == null || this.iv == null) return null;
				var params = this.userInfo;
				console.log(this.openid)
				params['openid'] = this.openid;
				params['encryptedData'] = this.encryptedData;
				params['iv'] = this.iv;
				api.register(params, res => {
					if (res.code === 200) {
						this.load = 0;
						let user = res.data.user;
						getApp().globalData.member = user;
						let access_token = res.data.token;
						uni.setStorageSync('loginStatus', 200);
						uni.setStorageSync('access_token', access_token);
						getApp().globalData.loginStatus = 1
						api.updateNowRegion({
							region_id: getApp().globalData.nowRegion.id
						}, res => {
							if(res.code === 200) {
								uni.navigateBack();
							}
						})
					} else if (res.code === 500) {
			
					} else {
						
					}
				});
			}
		},
		onShow: function() {
		},
		onLoad: function(options) {
			
			this.navH = getApp().globalData.navHeight;
			if(getApp().globalData.openid !== null) {
				this.openid = getApp().globalData.openid;
			}
			// if(options.from) {
			// 	this.from = options.from;
			// }
			// this.setting();
			// this.login();
		}
		
	}
</script>

<style scoped>
	.page-wrapper {
		display: flex;
		align-items: center;
		flex-direction: column;
		position: relative;
	}
	
	.go-back {
		width: 19rpx;
		height: 66rpx;
	}
	.go-back>image {
		width: 100%;
	}
	
	.content {
		width: 100%;
		flex-grow: 1;
		/* display: flex;
		align-items: center;
		justify-content: center; */
	}
	.content>div {
		width: 100%;
		flex-grow: 1;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		margin-top: 300rpx
	}
	.content>div>image {
		width: 260rpx;
	}
	.content>div>button {
		margin-top: 120rpx;
		background-image: linear-gradient(to right, #0973F7, #0286FA);
		width: 602rpx;
		height: 90rpx;
		border-radius: 45rpx;
		border:none;
		color: #ffffff;
		font-size: 34rpx;
	}
	.page-wrapper>p {
		width: 100%;
		display: flex;
		align-items: flex-start;
		justify-content: center;
		height: 80rpx;
		font-size: 22rpx;
		color: #666666;
	}
</style>
